package com.cyyang.controller;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyyang.shop.bean.Category;
import com.cyyang.shop.bean.Sku;
import com.cyyang.shop.bean.Spec;
import com.cyyang.shop.bean.Spu;
import com.cyyang.shop.service.CatService;
import com.cyyang.shop.service.SkuService;
import com.cyyang.shop.service.SpecService;
import com.cyyang.shop.service.SpuService;

@Controller
public class PortalController {
	
	@Reference
	CatService catService;
	
	@Reference
	SpuService spuService;
	
	@Reference
	SkuService skuService;
	
	@Reference
	SpecService specService;
	
	@RequestMapping("goodsx")
	@ResponseBody
	public List<Sku> goodsx(Integer spuId){
		return skuService.findSkuBySpuId(spuId);
	}
	
	@RequestMapping("goodsDetail")
	public String goodsDetail(Integer spuId,Model m) {
		//根据spuId查询Sku的集合,为了在在商品详情页展示
		List<Sku> skus = skuService.findSkuBySpuId(spuId);
		if(skus!=null && skus.size()>0) {
			Sku sku = skus.get(0);
			//获取该sku下的规格
			if(sku!=null) {
				List<Spec> specs = specService.findSpecsBySkuId(sku.getId());
				m.addAttribute("specs", specs);
			}
			
		}
		m.addAttribute("skus", skus);
		return "goodsDetail";
	}
	
	@RequestMapping("findSpuByCatId")
	public String findSpuByCatId(Integer catId,Model m) {
		List<Spu> spus = spuService.findSpuByCatId(catId);
		m.addAttribute("spus", spus);
		return "list";
	}
	
	@RequestMapping(value= {"","/","index"})
	public String index(Model m) {
		List<Category> cats = catService.getTree(0);
		m.addAttribute("cats", cats);
		return "index";
	}
}
